package com.tojabo.tree;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import com.tojabo.tree.BinTree.Node;

public class MainTest {

	
	public static void main(String[] args) throws IOException {
		int value=0;
		int key =0;
		BinTree binTree = new BinTree();
		
		binTree.insert(50, 1.8);
		binTree.insert(25, 1.2);
		binTree.insert(75, 3.7);
		binTree.insert(12, 1.5);
		binTree.insert(37, 4.6);
		
		binTree.insert(43, 1.9);
		binTree.insert(30, 5.5);
		binTree.insert(33, 6.8);
		binTree.insert(87, 3.5);
		binTree.insert(93, 1.6);
		
		binTree.insert(97, 9.15);
		
		while(true){
			System.out.println("Enter first letter of 'show',\n'insert','find','delete','traverse':");
			int choice = getChar();
			switch(choice){
			case 's':
				binTree.displayTree();
				break;
			case 'i':
				System.out.println("Enter value to insert:");
				value = getInt();
				binTree.insert(value, value+0.25);
				break;
			case 'f':
				System.out.println("Enter key to find:");
				key = getInt();
				Node found = binTree.find(key);
				if(found!=null){
					System.out.print("Found: ");
					found.displayNode();
				}else{
					System.out.println("Could not find: "+key);
				}
				break;
			case 'd':
				System.out.println("Enter key to delete:");
				key = getInt();
				boolean didDelete = binTree.delete(key);
				if(didDelete){
					System.out.println("done delete key="+key);
				}else{
					System.out.println("could not delete key="+key);
				}
				break;
			case 't':
				System.out.println("Enter type 1,2,3:");
				key = getInt();
				binTree.traverse(key);
				break;
			default:
				System.out.println("Invalid entry");
			}
		}
	}
	
	public static String getString() throws IOException {
		InputStreamReader isr = new InputStreamReader(System.in);
		BufferedReader br = new BufferedReader(isr);
		String s = br.readLine();
		return s;
	}
	
	public static char getChar() throws IOException{
		String s = getString();
		return s.charAt(0);
	}
	
	public static int getInt() throws IOException{
		String s = getString();
		return Integer.parseInt(s);
	}
}
